package com.lagou.edu.service.impl;

import com.lagou.edu.dao.UserDao;
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.UserRpcService;
import com.lagou.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public boolean emailRegistered(String email) {
        User user = new User();
        user.setEmail(email);
        return userDao.count(Example.of(user)) > 0;
    }

    @Override
    public User findByEmailAndPwd(String email, String pwd) {
        User user = new User();
        user.setEmail(email);
        user.setPwd(pwd);
        Optional<User> one = userDao.findOne(Example.of(user));
        return one.orElse(null);
    }

    @Override
    public void addUser(String email, String pwd) {
        User user = new User();
        user.setEmail(email);
        user.setPwd(pwd);
        userDao.save(user);
    }

}
